package vek.XML.Parsers;

import java.util.ArrayList;
import org.xml.sax.ContentHandler;
import org.xml.sax.helpers.DefaultHandler;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import vek.XML.parsedObjects.ParsedPOI;

/**
 * Parses the POI part of the XML files (from "< poi >" to "< /poi >") and saves the data read as a ParsedPOI object
 * @author Christian, Michal og Jakob (Hold 1, gruppe 2)
 */
public abstract class XMLParserPOI extends DefaultHandler implements ContentHandler {
    
    protected String tempElementData;
    
    private boolean parsingPOI;
    
    private String name;
    private String type;
    private int coordinatex = 0;
    private int coordinatey = 0;
    private String poiicon1;
    private String poiicon2;
    private String lastUpdated;
    protected ParsedPOI parsedPOI; // the parsedObject which contains the data from the XML. this is created by giving the above variables to the constructor of ParsedObject.
    
    protected ArrayList<ParsedPOI> returnedParsedObjects = new ArrayList<ParsedPOI>();

    /**
     * Runs each time the parser encounters a starting XML tag.
     * The parameters do not contain the data inside the tag, just the tag itself in various ways.
     * This implementation only uses the localName which in this implementation of the SAX parser equals the tag name
     * 
     * NOTE: the paramters change according to the features of the SAX parser.
     * In this implementation the localName is the name of the tag, and thus is the only parameter used 
     * 
     * @param uri not used, see XMLParserMain for more information
     * @param localName the name of the tag
     * @param qName note used see XMLParserMain for more information
     * @param atts note used, see XMLParserMain for more information
     * @throws org.xml.sax.SAXException
     */
    
    @Override
    public void startElement(String uri, String localName, String qName, Attributes atts) throws SAXException {
        
        if(localName.equals("poi"))
        {
            parsingPOI = true;
        }
        
        if(parsingPOI)
        {
            
        }
    }
    
    /**
     * Runs each time the parser encounter an end XML tag, see startElement()
     * @param uri not used, see XMLParserMain for more information
     * @param localName the name of the tag
     * @param qName not used, see XMLParserMain for more information
     * @throws org.xml.sax.SAXException
     */

    @Override
    public void endElement(String uri, String localName, String qName) throws SAXException {   
        
        if(localName.equals("poi"))
        {
            parsingPOI = false;
            parsedPOI = new ParsedPOI(name, type, coordinatex, coordinatey, poiicon1, poiicon2, lastUpdated);
        }
        else if(parsingPOI)
        {
            if(localName.equals("name"))
            {
                name = tempElementData;
            }
            else if(localName.equals("type"))
            {
                type = tempElementData;
            }
            else if(localName.equals("coordinateX"))
            {
                 coordinatex = Integer.parseInt(tempElementData);
            }
            else if(localName.equals("coordinateY"))
            {
                coordinatey = Integer.parseInt(tempElementData);
            }
            else if(localName.equals("poiicon1"))
            {
                poiicon1 = tempElementData;
            }
            else if(localName.equals("poiicon2"))
            {
                poiicon2 = tempElementData;
            }
            else if(localName.equals("lastupdated"))
            {
                lastUpdated = tempElementData;
            }
            
        }
        
    }
    
    /**
     * Runs each time data is encountered inside a tag.
     * Converts the characters to a string and saves it to the tempElementData variable
     * @param ch the data inside the tag as a char array
     * @param start
     * @param length the length of the char array
     * @throws org.xml.sax.SAXException
     */

    @Override
    public void characters(char[] ch, int start, int length) throws SAXException {
        
       tempElementData = new String(ch, start, length);
        
    }

 
}
